<template>
	<div class="app-box">
		<!-- 欢迎页面 -->
		<Welcome :key="componentKey" v-if="welcomeShow" @change="welcomChange"/>
		<template v-else>
			<Header  @change="headerChange"/>
			<div class="app-body-box">
				<Body />
			</div>
		</template>
	</div>
	<!-- <Header /> -->
</template>

<script setup lang="ts">
import { ref,onMounted } from "vue";
import { useStore } from 'vuex';
import Welcome from './components/Welcome.vue'
import Header from './components/Header.vue'
import Body from './components/Body.vue'
import { readProjectPathInfo } from './utils';


//获取当前组件实例，然后通过 proxy 属性访问 $bus
// const { proxy } = getCurrentInstance();
const store = useStore();

const componentKey = ref(0)
const welcomeShow=ref(false);

onMounted(async ()=>{

	/* //监听事件
	proxy.$bus.on('msg', (opts) => {

	}); */
	const projectInfo=await loadProjectList();
	/* // 同步检查文件夹是否存在
	if(fs.existsSync(projectInfo.entryProjectPath)) {
		// console.log('文件夹存在');
		return "";
	} */
	if(projectInfo.entryProjectPath==''){
		welcomeShow.value=true;
		componentKey.value+=1;
	}else{
		welcomeShow.value=false;
	}

})

const loadProjectList=async ()=>{

	//读取项目路径
	const projectInfo = await readProjectPathInfo('project.config.json');

	store.state.projectList=projectInfo.list;

	return projectInfo;
}

const welcomChange=(item)=>{
	/* if(item.type=="createProjectSuccess" || item.type=="importProjectSuccess" || item.type=="loadProject"){
		//保存新的项目路径信息
		saveProjectPathInfo('project.config.json', item.data.projectPath);
	} */
	welcomeShow.value=false;
	if(item.type=="createProjectSuccess"){
		//加载项目列表
		loadProjectList();
	}
}

const headerChange=(e)=>{
	if(e.type=="project"){
		welcomeShow.value=true;
	}
}

</script>

<style scoped>
	.app-box{
		display: flex;
		flex-direction:column;
		width:100%;
		height:100%;
	}

	.app-body-box{
		flex:1;
		display: flex;
		overflow: auto;
	}

</style>
